package me.siphone.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import java.util.List;
import me.siphone.http.api.QueryReplyList;
import org.linphone.R;

public class AnswerAdapter extends RecyclerView.Adapter<AnswerAdapter.AnswerViewHolder> {

    private List<QueryReplyList.Reply> replyList;

    public AnswerAdapter(List<QueryReplyList.Reply> replyList) {
        this.replyList = replyList;
    }

    public void update(List<QueryReplyList.Reply> replyList) {
        this.replyList = replyList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AnswerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AnswerViewHolder(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_answer_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AnswerViewHolder holder, int position) {
        QueryReplyList.Reply reply = replyList.get(position);
        Log.d("sisyphus", "onBindViewHolder: " + new Gson().toJson(reply));
        holder.tvAnswerName.setText(reply.getNumber());
        holder.tvAnswerStatus.setText(reply.getReplyStatus() == 1 ? "已应答" : "未应答");
        holder.tvAnswerStatus.setTextColor(
                reply.getReplyStatus() == 1
                        ? Color.parseColor("#09bb07")
                        : Color.parseColor("#e2de02"));
        holder.tvAnswerTime.setText(
                TextUtils.isEmpty(reply.getReplyTime()) ? "00:00:00" : reply.getReplyTime());
    }

    @Override
    public int getItemCount() {
        return replyList.size();
    }

    public static class AnswerViewHolder extends RecyclerView.ViewHolder {

        TextView tvAnswerName;
        TextView tvAnswerStatus;
        TextView tvAnswerTime;

        public AnswerViewHolder(@NonNull View itemView) {
            super(itemView);
            tvAnswerName = itemView.findViewById(R.id.tv_answer_name);
            tvAnswerStatus = itemView.findViewById(R.id.tv_answer_status);
            tvAnswerTime = itemView.findViewById(R.id.tv_answer_time);
        }
    }
}
